﻿using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;
using System.Text;
using Galoap.JSON;
using Galoap;
using Galoap.Properties;
using Galoap.UnitTest.Storage;
using Galoap.Questions;
using Galoap.UnitTest;
using Galoap.Workflow;
using Galoap.Workflow.Model;
using Galoap.Workflow.ParseScxml;

public partial class Login : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
        Hashtable lista = new Hashtable();
        string risposta = "{\"risposta\":\"false\", \"user_id\":null}";
        bool login = false;
        string richiesta = Request.Params[0].ToString();
        lista = (Hashtable)JSON.JsonDecode(richiesta);
        try  {
            string type = (string)lista["event"];
            if(type == "login"){
                string nick = (string)lista["login"];
                string pass = (string)lista["password"];
                Galoap.User logged = null;
                try   {
                    logged = Galoap.User.Login(nick, pass);
                    login = true;
                }
                catch (System.ArgumentException)  {
                    login = false;
                }

                if (login) {
                        risposta = "{\"risposta\":\"true\", \"user_id\":\"1\"}";
                        Parser p = new Parser();
                        //string sPathG = @"C:\Users\Giacomo\Documents\Visual Studio 2008\Projects\galoap\Galoap\Workflow\Xml\dualstat.xml";
                        string sPathM = @"C:\Users\simo\Documents\Visual Studio 2008\Projects\galoap\Galoap\Workflow\Xml\dualstat.xml";
                        Workflow obj = p.parse(sPathM);
                        Session["Workflow"] = obj;
                        Session["Nick"] = nick;
                }
            }
        }
        catch (NullReferenceException) { }
        Response.Write(risposta);
        Response.End();
    }

    public string md5(string input)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
        bs = x.ComputeHash(bs);
        System.Text.StringBuilder s = new System.Text.StringBuilder();
        foreach (byte b in bs)
        {
            s.Append(b.ToString("x2").ToLower());
        }
        string password = s.ToString();
        return password;
    }
}
